<!--
 * @Author: wangming
 * @Date: 2020-09-09 14:04:55
 * @LastEditors: wangming
 * @LastEditTime: 2022-05-20 17:42:20
 * @Description:添加类别按钮
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="560px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>自定义标签</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form class="pl30 pr30" :model="FormData" ref="Form" label-width="120px">
      <el-form-item
        label="标签名称"
        prop="label"
        :rules="[
          { required: true, message: '标签名称不能为空', trigger: 'blur' },
        ]"
      >
        <el-input
          style="width: 300px"
          v-model="FormData.label"
          placeholder="请输入选项名称"
          class="ml10"
        ></el-input>
      </el-form-item>
      <el-form-item label="标签ID" required>
        <el-input
          style="width: 300px"
          v-model="FormData.value"
          disabled
          class="ml10"
        ></el-input>
      </el-form-item>
      <el-form-item label="隐藏标签">
        <el-switch v-model="FormData.hide"></el-switch>
      </el-form-item>
      <Ruleconditions :FormData="FormData"></Ruleconditions>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Ruleconditions from "../../__comModule__/Ruleconditions";
export default {
  components: { Ruleconditions },
  data() {
    return {
      FormData: {
        label: "",
        value: "",
        label_i18n: "",
        hide: false,
        RuleConditions: [],
      },
      showVisible: false,
      callback: () => {},
      validator: (rule, value, callback) => {
        if (this.FormData.list.filter((i) => i.value == value).length > 1) {
          callback(new Error("ID 不能重复"));
        } else {
          callback();
        }
      },
    };
  },
  methods: {
    editForm({ label, value, label_i18n, hide, RuleConditions }) {
      this.FormData.label = label;
      this.FormData.value = value;
      this.FormData.label_i18n = label_i18n;
      this.FormData.hide = !!hide;
      this.FormData.RuleConditions = RuleConditions || [];
      return this.showModel();
    },
    showModel() {
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.Form.validate();
        this.showVisible = false;
        this.callback(this.FormData);
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped></style>
